package com.gzdh.dishrecognition.ui.fragment.sysinfo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.dhkj.web.bean.KlStateRes;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.ui.adapter.info.KlInfoAdapter;
import com.gzdh.dishrecognition.ui.dialog.KlDialog;
import com.gzdh.dishrecognition.ui.widget.SwipeLayoutManager;
import com.gzdh.dishrecognition.util.SystemUtils;

import java.util.List;

public class KlFragment extends Fragment {
    private Activity activity;
    private Context mContext;

    private KlInfoAdapter klInfoAdapter;

    public KlFragment(Activity activity) {
        this.activity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = inflater.getContext();
        klInfoAdapter = new KlInfoAdapter(mContext);
        View view = getViews(inflater);
        return view;
    }

    private View getViews(LayoutInflater inflater) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout mainLayout = new LinearLayout(inflater.getContext());
        mainLayout.setLayoutParams(layoutParams);
        mainLayout.setBackgroundColor(Color.WHITE);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        mainLayout.addView(getTitleView());   //标题初始化
        mainLayout.addView(getBody());        //主题内容
        return mainLayout;
    }


    private View getTitleView() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout titleLinear = new LinearLayout(mContext);
        titleLinear.setLayoutParams(layoutParams);
        titleLinear.setBackgroundColor(Color.WHITE);
        titleLinear.setOrientation(LinearLayout.VERTICAL);

        RelativeLayout titleRv = new RelativeLayout(mContext);
        titleRv.setLayoutParams(layoutParams);
        titleLinear.addView(titleRv);

        //-------------------------标题
        TextView titleTv = new TextView(mContext);
        titleTv.setText("卡 类 查 询 ");
        RelativeLayout.LayoutParams tt1 = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        int tt1Margins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, this.getResources().getDisplayMetrics());
        tt1.setMargins(tt1Margins, tt1Margins, tt1Margins, tt1Margins);
        tt1.addRule(RelativeLayout.CENTER_IN_PARENT);
        titleTv.setLayoutParams(tt1);
        titleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        titleRv.addView(titleTv);

        //--------------------------右侧新增按钮
        TextView addBut = new TextView(mContext);
        RelativeLayout.LayoutParams addButParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        int addButParamsMargins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, this.getResources().getDisplayMetrics());
        addButParams.setMargins(0, 0, addButParamsMargins, 0);
        addButParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        addButParams.addRule(RelativeLayout.CENTER_VERTICAL);
        addBut.setText(getResources().getString(R.string.add));
        addBut.setLayoutParams(addButParams);
        addBut.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
        addBut.setTextColor(Color.WHITE);
        addBut.setBackground(getResources().getDrawable(R.drawable.bg_btn_pay));
        addBut.setPadding(15, 0, 15, 0);
        titleRv.addView(addBut);

        addBut.setOnClickListener(view -> {
            KlDialog klDialog = new KlDialog(mContext);
            klDialog.show();
        });

        //----------------------------下划线
        View view = new View(mContext);
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        view.setBackgroundColor(Color.rgb(224, 224, 224));
        titleLinear.addView(view);

        return titleLinear;
    }

    private View getBody() {
        LinearLayout mainLinear = new LinearLayout(mContext);
        mainLinear.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        mainLinear.setBackground(getResources().getDrawable(R.drawable.background_w));
        mainLinear.setOrientation(LinearLayout.VERTICAL);

        LinearLayout topLinear = new LinearLayout(mContext);
        topLinear.setOrientation(LinearLayout.HORIZONTAL);
        topLinear.setBackgroundColor(Color.rgb(240, 240, 240));
        topLinear.setPadding(0, 15, 0, 15);
        topLinear.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        mainLinear.addView(topLinear);

        LinearLayout.LayoutParams topLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);

        TextView xuhaoTopTv = new TextView(mContext);
        xuhaoTopTv.setText("序号");
        xuhaoTopTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        xuhaoTopTv.setLayoutParams(topLayoutParams);
        xuhaoTopTv.setGravity(Gravity.CENTER);
        topLinear.addView(xuhaoTopTv);

        TextView bmTopTv = new TextView(mContext);
        bmTopTv.setText("编码");
        bmTopTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        bmTopTv.setLayoutParams(topLayoutParams);
        bmTopTv.setGravity(Gravity.CENTER);
        topLinear.addView(bmTopTv);

        TextView nameTopTv = new TextView(mContext);
        nameTopTv.setText("卡类名称");
        nameTopTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        nameTopTv.setLayoutParams(topLayoutParams);
        nameTopTv.setGravity(Gravity.CENTER);
        topLinear.addView(nameTopTv);


        ListView listView = new ListView(mContext);
        listView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        listView.setAdapter(klInfoAdapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                    //如果垂直滑动，则需要关闭已经打开的layout
                    SwipeLayoutManager.getInstance().closeCurrentLayout();
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        klInfoAdapter.setUserClick(klStateRes -> {
            //发起删除
            RequestUtils.deleteKl(mContext, klStateRes.getId(), SystemUtils.getSerialNumber(), new MyObserverRes<List<KlStateRes>>(mContext) {
                @Override
                public void onSuccess(List<KlStateRes> result) {
                    klInfoAdapter.setList(result);
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {

                }
            });
        });
        mainLinear.addView(listView);
        return mainLinear;
    }

    @Override
    public void onResume() {
        super.onResume();
        RequestUtils.getKlState(activity, SystemUtils.getSerialNumber(), new MyObserverRes<List<KlStateRes>>(activity) {
            @Override
            public void onSuccess(List<KlStateRes> result) {
                klInfoAdapter.setList(result);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {

            }
        });
    }

    @Override
    public void onPause() {
        SwipeLayoutManager.getInstance().clearCurrentLayout();
        super.onPause();
    }

}
